﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using COC.Commons;

namespace COC.Tool
{
    public partial class CodingPlanView : Form
    {
        private readonly CodingPlan _CodingPlan = new CodingPlan();
        private readonly List<PlanTask> _PlanTasks;
        private readonly PlanTaskManagerConfigView _PlanTaskManagerConfigView = new PlanTaskManagerConfigView();
        private readonly PlanTaskManagerConfig _PlanTaskManagerConfig = new PlanTaskManagerConfig();

        public CodingPlanView()
        {
            InitializeComponent();
            //导入状态
            FreshenState.Items.Insert(0,"全   部");
            for (int i = 0; i < _PlanTaskManagerConfig.TaskState.Count; i++)
            {
                
                FreshenState.Items.Insert(i+1, _PlanTaskManagerConfig.TaskState[i]);
            }
            //导入类别
            FreshenType.Items.Insert(0, "全部");
            for (int i = 0; i < _PlanTaskManagerConfig.TaskType.Count; i++)
            {
                FreshenType.Items.Insert(i+1, _PlanTaskManagerConfig.TaskType[i]);
            }

            //判断路径是否有效，无效就弹出提示和配置窗口，配置之后变量改变再重新判断直到是可用

            //判断路径下是否有文件，如果没有文件就弹出提示是否创建一个空的任务库

            _PlanTasks = _CodingPlan.PlanTasks;
            FreshenState.SelectedIndex = 1;
            FreshenType.SelectedIndex = 0;
            FreshenAllListView();
        }

        /// <summary>
        /// 刷新整个_DataGridView任务列表的内容
        /// </summary>
        private void FreshenAllListView()
        {
            _DataGridView.Rows.Clear();
            string freshenstate = FreshenState.Text;
            string freshentype = FreshenType.Text;

            if (freshenstate.Equals("全   部"))
            {
                foreach (var plan in _PlanTasks)
                {
                    if(freshentype.Equals("全部"))
                    {
                        var index = _DataGridView.Rows.Add();
                        FillRow(index, plan);
                    }
                    else
                    {
                        if(plan.Type.Equals(freshentype))
                        {
                            var index = _DataGridView.Rows.Add();
                            FillRow(index, plan);
                        }
                    }
                }
            }
            else
            {
                foreach (var plan in _PlanTasks)
                {
                    if (plan.State.Equals(freshenstate))
                    {
                        if (freshentype.Equals("全部"))
                        {
                            var index = _DataGridView.Rows.Add();
                            FillRow(index, plan);
                        }
                        else
                        {
                            if (plan.Type.Equals(freshentype))
                            {
                                var index = _DataGridView.Rows.Add();
                                FillRow(index, plan);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 刷新单行内容
        /// </summary>
        /// <param name="index">行索引</param>
        private void FreshenRowView(int index)
        {
            string pid = _DataGridView.Rows[index].Cells["Id"].Value.ToString();
            var plan = _CodingPlan.GetTaskFromId(_PlanTasks, pid);
            FillRow(index, plan);
        }

        /// <summary>
        /// 填充_DataGridView任务列表的行数据
        /// </summary>
        /// <param name="index">行索引</param>
        /// <param name="plan">PlanTask任务</param>
        private void FillRow(int index,PlanTask plan)
        {
            _DataGridView.Rows[index].Cells["Id"].Value = plan.Id;
            _DataGridView.Rows[index].Cells["Content"].Value = plan.Content;
            _DataGridView.Rows[index].Cells["type"].Value = plan.Type;
            _DataGridView.Rows[index].Cells["state"].Value = plan.State;
            _DataGridView.Rows[index].Cells["insdata"].Value = plan.InsData;
            _DataGridView.Rows[index].Cells["fulfilldata"].Value = plan.FulFillData;
            _DataGridView.Rows[index].Cells["comment"].Value =
                plan.Comments.Count == 0 ? "——" : plan.Comments.Last().Content;
            _DataGridView.Rows[index].Cells["addComment"].Value = "添加备注";
            _DataGridView.Rows[index].Cells["allComments"].Value = "所有备注";
            _DataGridView.Rows[index].Cells["edit"].Value = "编辑";
        }

        /// <summary>
        ///     点击列中的按键弹出对话框效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            var column = _DataGridView.Columns[e.ColumnIndex];

            //点击添加备注
            if (column is DataGridViewButtonColumn && column.Name == "addComment")
            {
                var myid = _DataGridView.Rows[e.RowIndex].Cells["Id"].Value.ToString();
                var addCommentView = new AddCommentView(_CodingPlan.GetTaskFromId(_PlanTasks, myid));
                addCommentView.ShowDialog();
                FreshenRowView(e.RowIndex);
            }

            //点击所有备注
            if (column is DataGridViewButtonColumn && column.Name == "allComments")
            {
                string myid = _DataGridView.Rows[e.RowIndex].Cells["Id"].Value.ToString();
                var taskCommentListView = new TaskCommentListView(_CodingPlan.GetTaskFromId(_PlanTasks, myid));
                taskCommentListView.ShowDialog();
                FreshenRowView(e.RowIndex);
            }

            //点击编辑
            if (column is DataGridViewButtonColumn && column.Name == "edit")
            {
                string myid = _DataGridView.Rows[e.RowIndex].Cells["Id"].Value.ToString();
                var newPlanTaskView = new NewPlanTaskView(_PlanTasks, myid);
                newPlanTaskView.ShowDialog();
                FreshenRowView(e.RowIndex);
            }
        }

        private void AddNewPlanTask_Click(object sender, EventArgs e)
        {
            var newPlanTaskView = new NewPlanTaskView(_PlanTasks, "0");
            newPlanTaskView.ShowDialog();
            FreshenAllListView();
        }

        private void FreshenPlanListView_Click(object sender, EventArgs e)
        {
            FreshenAllListView();
        }

        private void FreshenState_SelectedIndexChanged(object sender, EventArgs e)
        {
            FreshenAllListView();
        }

        private void FreshenType_SelectedIndexChanged(object sender, EventArgs e)
        {
            FreshenAllListView();
        }

        private void PlanTaskManagerConfigView_Click(object sender, EventArgs e)
        {
            _PlanTaskManagerConfigView.ShowDialog();
        }
    }
}